// Author: Domien Nowicki
#include "DefaultTextureWidget.h"

DefaultTextureWidget::DefaultTextureWidget(QWidget *parent): QWidget(parent)
{
	SetupWidget();
}

DefaultTextureWidget::~DefaultTextureWidget()
{
}

void DefaultTextureWidget::SetupWidget()
{
    hboxLayout = new QHBoxLayout(this);
    hboxLayout->setSpacing(6);
    hboxLayout->setMargin(0);

	// Add 3 boxes with default texture selectors
	QPushButton *defaultButton = SetupDefaultTexture(DEFAULT, "Default texture:");
	QPushButton *floorButton = SetupDefaultTexture(FLOOR, "Default floor texture:");
	QPushButton *ceilingButton = SetupDefaultTexture(CEILING, "Default ceiling texture:");


	QObject::connect(defaultButton, SIGNAL(clicked()), this, SLOT(slotChangeDefaultTexture()));
	QObject::connect(floorButton, SIGNAL(clicked()), this, SLOT(slotChangeFloorTexture()));
	QObject::connect(ceilingButton, SIGNAL(clicked()), this, SLOT(slotChangeCeilingTexture()));


	spacerItem = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
	hboxLayout->addItem(spacerItem);
}

QPushButton* DefaultTextureWidget::SetupDefaultTexture(DefaultTexture texture, const char* caption)
{
	// Create a new framed box, with an image and a change button

    QGroupBox *groupBox = new QGroupBox(this);
    QVBoxLayout *vboxLayout = new QVBoxLayout(groupBox);
    QLabel *label = new QLabel(groupBox);
    QPushButton *button = new QPushButton(groupBox);

	vboxLayout->setSpacing(6);
    vboxLayout->setMargin(9);
    vboxLayout->addWidget(label);
    vboxLayout->addWidget(button);
    hboxLayout->addWidget(groupBox);

    groupBox->setTitle(caption);
	button->setText("Change...");
	button->setIcon(QPixmap("icons/changefile.png"));
	button->setStatusTip("Change default texture");
	button->setToolTip("Change default texture");

	label->clear();
	label->setScaledContents(false);
	label->setAlignment(Qt::AlignHCenter);

	// Add the widgets to the array (for changing them later on)
	int index = (int)texture;
    defaultTextureBox[index] = groupBox;
    vboxDefaultTextureLayout[index] = vboxLayout;
    defaultTextureLabel[index] = label;
    defaultTextureButton[index] = button;

	return button;
}

void DefaultTextureWidget::SetDefaultTexture(DefaultTextureWidget::DefaultTexture texture, const QPixmap& thumb)
{
	// Change thumb
	defaultTextureLabel[(int)texture]->setPixmap(thumb);
}

void DefaultTextureWidget::slotChangeDefaultTexture()
{
	emit changeTexture(DEFAULT);
}

void DefaultTextureWidget::slotChangeFloorTexture()
{
	emit changeTexture(FLOOR);
}

void DefaultTextureWidget::slotChangeCeilingTexture()
{
	emit changeTexture(CEILING);
}
